package cn.com.chec.en.controller.front;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.com.chec.en.dao.NewsInfoDao;
import cn.com.chec.en.dao.PageLinkDao;
import cn.com.chec.en.model.domain.ChannelInfo;
import cn.com.chec.en.model.domain.NewsInfo;
import cn.com.chec.en.model.domain.QueryNewsObject;
import cn.com.chec.en.model.dto.NewsDTO;
import cn.com.chec.en.util.ContactUtils;
import cn.com.chec.en.util.PageBean;

@Controller
public class StoryFrontController {
	protected static final Logger logger = Logger
			.getLogger(StoryFrontController.class);

	@Resource
	private PageLinkDao pageLinkDao;

	@Resource
	private NewsInfoDao newsInfoDao;

	@Resource
	private Map<String, Object> environment;

	@RequestMapping({ "/story" })
	public ModelAndView list(
			@RequestParam(value = "curPage", defaultValue = "1", required = false) String curPage,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("story");

		Map<String, String> pageLinkMap = this.pageLinkDao.getAllAsMap();
		logger.info("[NewsFrontController] pageLinkMap=" + pageLinkMap);
		map.put("pageLinkMap", pageLinkMap);

		map.putAll(ContactUtils.getContactMap());

		PageBean pageBean = new PageBean();
		int curPageInt = (StringUtils.isNotBlank(curPage))
				&& (curPage.matches("\\d+")) ? Integer.parseInt(curPage) : 1;
		pageBean.setCurPage(curPageInt);
		QueryNewsObject queryObj = new QueryNewsObject();
		queryObj.setLanguageId(1);
		queryObj.setIsShowInList(1);
		queryObj.setIsReview(1);
		queryObj.setStart(pageBean.getStart());
		queryObj.setNum(pageBean.getRowsPerPage());
		List<NewsDTO> newsList = this.newsInfoDao
				.getNewsListByQueryPChannelIdDistinct(queryObj, 4);
		pageBean.setMaxRowCount(this.newsInfoDao
				.getNewsCountByQueryPChannelIdDistinct(queryObj, 4));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		map.put("newsInfoDTOList", newsList);
		map.put("pageBean", pageBean);

		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping(value = { "/story/{id}" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView detail(@PathVariable String id,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("storyDetail");

		Map<String, String> pageLinkMap = this.pageLinkDao.getAllAsMap();
		logger.info("[NewsFrontController] pageLinkMap=" + pageLinkMap);
		map.put("pageLinkMap", pageLinkMap);

		map.putAll(ContactUtils.getContactMap());

		int storyId = (StringUtils.isNotBlank(id)) && (id.matches("\\d+")) ? Integer
				.parseInt(id) : 0;
		NewsInfo newsInfo = this.newsInfoDao.getNewsInfoByNewsId(storyId);

		List<ChannelInfo> allList = newsInfo.getChannelInfoList();
		List<ChannelInfo> busiList = new ArrayList<ChannelInfo>();
		List<ChannelInfo> zhengshuList = new ArrayList<ChannelInfo>();
		for (ChannelInfo c : allList) {
			if (c.getChannelParentId() == 2) {
				busiList.add(c);
			}
			if (c.getChannelParentId() == 31) {
				zhengshuList.add(c);
			}
		}

		map.put("newsInfo", newsInfo);
		map.put("busiList", busiList);
		map.put("zhengshuList", zhengshuList);

		map.putAll(this.environment);
		mav.addAllObjects(map);

		return mav;
	}
}